package com.settlement.system.common.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class DateUtils {

    private static final Logger logger = LoggerFactory.getLogger(DateUtils.class);

    public static final DateTimeFormatter crossDf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    static final DateTimeFormatter diagonalDf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    static final DateTimeFormatter version_df = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");

    public static LocalDateTime convert(String date) {
        try {
            if (!StringUtils.hasText(date)) {
                return null;
            }
            if (date.length() < 17) {
                date = "%s:00".formatted(date);
            }
            if (date.contains("T")) {
                date = StringUtils.replaceStr(date, "T", " ");
            }
            if (date.contains("-")) {
                return LocalDateTime.parse(date, crossDf);
            } else if (date.contains("/")) {
                return LocalDateTime.parse(date, diagonalDf);
            } else {
                return null;
            }
        } catch (Exception e) {
            return null;
        }
    }

    public static String getDateString() {
        return LocalDateTime.now().format(version_df);
    }

    public static String getDateString(DateTimeFormatter df) {
        return LocalDateTime.now().format(df);
    }

    public static String getDateString(LocalDateTime date, DateTimeFormatter df) {
        return date.format(df);
    }

    public static LocalDate convertLocalDate(String date, String pattern) {
        try {
            DateTimeFormatter df = DateTimeFormatter.ofPattern(pattern);
            return LocalDate.parse(date, df);
        } catch (Exception e) {
            return null;
        }
    }

    public static String formatDate(LocalDateTime date) {
        try {
            DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMM");
            return date.format(df);
        } catch (Exception e) {
            return null;
        }
    }

    public static List<String> getLastAndNextBillDateList(String billMonth) {
        List<String> dateList = new ArrayList<>();
        dateList.add(billMonth);
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMdd");
        LocalDate localDateTime = LocalDate.parse("%s01".formatted(billMonth), df);
        dateList.add(formatDate(localDateTime.plusMonths(1).atStartOfDay()));
        dateList.add(formatDate(localDateTime.plusMonths(2).atStartOfDay()));
        dateList.add(formatDate(localDateTime.plusMonths(3).atStartOfDay()));
        dateList.add(formatDate(localDateTime.plusMonths(4).atStartOfDay()));
        dateList.add(formatDate(localDateTime.minusMonths(1).atStartOfDay()));
        dateList.add(formatDate(localDateTime.minusMonths(2).atStartOfDay()));
        dateList.add(formatDate(localDateTime.minusMonths(3).atStartOfDay()));
        dateList.add(formatDate(localDateTime.minusMonths(4).atStartOfDay()));
        return dateList;
    }

    // 通过此方法，将字符串的日期格式化
    public static LocalDateTime convertStringToLocalDateTime(String text) {
        try {
            if (!StringUtils.hasText(text) || text.equalsIgnoreCase("null")) {
                return null;
            }
            text = text.replaceAll("T", " ");
            String[] dateArr = text.split(" ");
            String dateStr = dateArr[0];
            String timeStr;
            if (dateArr.length > 1) {
                timeStr = dateArr[dateArr.length - 1];
            } else {
                timeStr = "00:00:00";
            }
            dateStr = StringUtils.replaceStr(dateStr, "/", "-");
            dateStr = StringUtils.replaceStr(dateStr, "\\.", "-");
            List<String> dateList = new ArrayList<>();
            List<String> timeList = new ArrayList<>();
            if(!dateStr.contains("-")){
                dateStr = "%s-%s-%s".formatted(dateStr.substring(0, 4), dateStr.substring(4, 6), dateStr.substring(6, 8));
            }
            String[] dateSplit = dateStr.split("-");
            for (String str : dateSplit) {
                if (str.trim().length() < 2) {
                    dateList.add("0" + str.trim());
                } else {
                    dateList.add(str.trim());
                }
            }
            timeStr = timeStr.replaceAll("：", ":");
            dateSplit = timeStr.split(":");
            for (String str : dateSplit) {
                if (str.trim().length() < 2) {
                    timeList.add("0" + str.trim());
                } else {
                    timeList.add(str.trim());
                }
            }
            if (timeList.size() < 3) {
                timeList.add("00");
            }
            return LocalDateTime.parse("%s %s".formatted(String.join("-", dateList), String.join(":", timeList)), crossDf);
        } catch (Exception e) {
            logger.error("日期转换异常：{}", e.getMessage());
            return null;
        }
    }

}
